package com.example.wuyeguanli.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.wuyeguanli.R;
import com.example.wuyeguanli.entiy.Repair;
import com.example.wuyeguanli.entiy.Response;
import com.example.wuyeguanli.tools.ApiClient;
import com.example.wuyeguanli.tools.DataValidator;
import com.example.wuyeguanli.tools.RepairAdapter;

import java.util.ArrayList;
import java.util.List;

public class RepairlistActivity extends AppCompatActivity {
    private final String TAG = "ApiClient";
    private RecyclerView recyclerView;
    private RepairAdapter adapter;
    private List<Repair> repairs;
    private Button btn_add;
    private Button btn_back;
    private EditText edit_room;
    private Button btn_search;
    private ProgressBar loading_progress;
    private final ApiClient apiClient = new ApiClient();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repairlist);

        //隐藏标题栏
        if(getSupportActionBar() != null){
            getSupportActionBar().hide();
        }

        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setHasFixedSize(true); // 提高性能
        btn_add = findViewById(R.id.btn_add);
        btn_back = findViewById(R.id.btn_back);
        edit_room = findViewById(R.id.edit_room);
        btn_search = findViewById(R.id.btn_search);
        loading_progress = findViewById(R.id.loading_progress);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        repairs = new ArrayList<>();
        adapter = new RepairAdapter(repairs, this);
        recyclerView.setAdapter(adapter);

        btn_back.setOnClickListener(v -> finish());

        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setLoadingState(true);

                if (!DataValidator.isValidRoomNumber(edit_room.getText().toString())) {
                    setLoadingState(false);
                    Toast.makeText(RepairlistActivity.this, "房间号要求6位数字", Toast.LENGTH_SHORT).show();
                    return;
                }

                apiClient.getRepairByRoomNumber(edit_room.getText().toString(), new ApiClient.OnCompleteListener<Response<List<Repair>>>() {
                    @Override
                    public void onSuccess(Response<List<Repair>> data) {
                        setLoadingState(false);
                        if (data.getState().equals("success")) {
                            repairs.clear();
                            repairs.addAll(data.getData());
                            adapter.notifyDataSetChanged();
                            Toast.makeText(RepairlistActivity.this, "报修列表加载成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(RepairlistActivity.this, data.getError(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void OnError(Throwable error) {
                        setLoadingState(false);
                        Log.e(TAG, "OnError: ", error);
                        Toast.makeText(RepairlistActivity.this, "网络错误，请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RepairlistActivity.this, RepairApplyActivity.class);
                startActivity(intent);
            }
        });
    }

    private void setLoadingState(boolean isLoading) {
        if (isLoading) {
            loading_progress.setVisibility(View.VISIBLE);
            recyclerView.setEnabled(false);
            edit_room.setEnabled(false);
            btn_search.setEnabled(false);
            btn_add.setEnabled(false);
        } else {
            loading_progress.setVisibility(View.GONE);
            recyclerView.setEnabled(true);
            edit_room.setEnabled(true);
            btn_search.setEnabled(true);
            btn_add.setEnabled(true);
        }
    }
}
